/**
 * 模拟数据模块
 * 用于在Android接口不可用时提供模拟数据
 */

console.log('📦 模拟数据模块已加载');

// 模拟设备列表
const mockDeviceList = [
    { 
        id: 1, 
        name: 'PLC设备1', 
        ip: '192.168.0.1',
        port: 502, 
        type: 'Siemens S7-1200', 
        typeId: 1,
        unitId: 1,
        status: 'online',
        description: '1号机房主控PLC'
    },
    {
        id: 2,
        name: 'PLC设备2',
        ip: '192.168.0.51',
        port: 502,
        type: 'Mitsubishi FX3U',
        typeId: 2,
        unitId: 1,
        status: 'offline',
        description: '温湿度控制PLC'
    },
    {
        id: 3,
        name: 'PLC设备3',
        ip: '192.168.0.7',
        port: 23,
        type: 'Omron CP1E',
        typeId: 3,
        unitId: 1,
        status: 'online',
        description: '粮库仓储监控系统'
    }
];

// 模拟设备类型列表
const mockDeviceTypes = [
    { id: 1, name: '西门子 S7-1200', englishName: 'Siemens S7-1200' },
    { id: 2, name: '三菱 FX3U', englishName: 'Mitsubishi FX3U' },
    { id: 3, name: '欧姆龙 CP1E', englishName: 'Omron CP1E' },
    { id: 4, name: 'Modbus TCP', englishName: 'Modbus TCP' },
    { id: 5, name: '其它', englishName: 'Other' }
];

// 导出所有模拟数据
window.MockData = {
    deviceList: mockDeviceList,
    deviceTypes: mockDeviceTypes,
    
    // 根据ID获取设备详情
    getDeviceById: function(deviceId) {
        console.log('📦 使用模拟数据获取设备: ID =', deviceId);
        return mockDeviceList.find(device => device.id.toString() === deviceId.toString());
    },
    
    // 获取所有设备列表
    getAllDevices: function() {
        console.log('📦 使用模拟数据获取所有设备');
        return mockDeviceList;
    },
    
    // 获取所有设备类型
    getAllDeviceTypes: function() {
        console.log('📦 使用模拟数据获取所有设备类型');
        return mockDeviceTypes;
    },
    
    // 添加设备
    addDevice: function(deviceData) {
        console.log('📦 使用模拟数据添加设备:', deviceData);
        const newId = mockDeviceList.length > 0 ? 
            Math.max(...mockDeviceList.map(device => device.id)) + 1 : 1;
        
        const newDevice = {
            id: newId,
            name: deviceData.deviceName || deviceData.name,
            ip: deviceData.deviceIp || deviceData.ip,
            port: deviceData.devicePort || deviceData.port,
            type: deviceData.deviceType || deviceData.type,
            typeId: deviceData.deviceTypeId || deviceData.typeId,
            unitId: deviceData.unitId,
            status: 'offline',
            description: deviceData.description || ''
        };
        
        mockDeviceList.push(newDevice);
        return 'success';
    },
    
    // 更新设备
    updateDevice: function(deviceData) {
        console.log('📦 使用模拟数据更新设备:', deviceData);
        const deviceId = deviceData.deviceId || deviceData.id;
        const index = mockDeviceList.findIndex(device => device.id.toString() === deviceId.toString());
        
        if (index === -1) return '设备不存在';
        
        mockDeviceList[index] = {
            ...mockDeviceList[index],
            name: deviceData.deviceName || deviceData.name || mockDeviceList[index].name,
            ip: deviceData.deviceIp || deviceData.ip || mockDeviceList[index].ip,
            port: deviceData.devicePort || deviceData.port || mockDeviceList[index].port,
            type: deviceData.deviceType || deviceData.type || mockDeviceList[index].type,
            typeId: deviceData.deviceTypeId || deviceData.typeId || mockDeviceList[index].typeId,
            unitId: deviceData.unitId || mockDeviceList[index].unitId
        };
        
        return 'success';
    },
    
    // 删除设备
    deleteDevice: function(deviceId) {
        console.log('📦 使用模拟数据删除设备: ID =', deviceId);
        const index = mockDeviceList.findIndex(device => device.id.toString() === deviceId.toString());
        
        if (index === -1) return '设备不存在';
        
        mockDeviceList.splice(index, 1);
        return 'success';
    },
    
    // 测试设备连接
    testDeviceConnection: function(deviceData) {
        console.log('📦 使用模拟数据测试设备连接:', deviceData);
        // 模拟80%的成功率
        return Math.random() > 0.2 ? 'success' : '连接失败';
    }
}; 